package com.rul.mars.user.controller;

import com.rul.mars.common.api.CommonResult;
import com.rul.mars.mbg.domain.ReceiveAddressDetail;
import com.rul.mars.user.service.UserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收货地址服务接口
 *
 * @author RuL
 */
@Api(tags = "ReceiveAddressController", description = "用户收货地址服务接口")
@RestController
@RequestMapping(path = "/user/address")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    /**
     * 根据收货地址ID查询收货地址详细信息
     *
     * @param id 收货地址ID
     * @return 收货地址详细信息
     */
    @ApiOperation("根据收货地址ID查询收货地址详细信息")
    @GetMapping("/detail/{id}")
    public CommonResult detail(@PathVariable(value = "id") Long id) {
        ReceiveAddressDetail addressDetail = userAddressService.detail(id);
        return CommonResult.success(addressDetail);
    }

    /**
     * 查询用户所有地址信息
     *
     * @return 用户所有地址信息
     */
    @ApiOperation("根据收货地址ID查询收货地址详细信息")
    @GetMapping("/listForUser")
    public CommonResult<List<ReceiveAddressDetail>> listAddressForUser() {
        List<ReceiveAddressDetail> details = userAddressService.listAddressDetail();
        return CommonResult.success(details);
    }


    /**
     * 根据收货地址ID删除收货地址
     *
     * @param id 收货地址ID
     * @return 操作结果
     */
    @ApiOperation("删除收货地址")
    @GetMapping("/delete")
    public CommonResult deleteAddress(@RequestParam(value = "id") Long id) {
        if (userAddressService.deleteAddress(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    /**
     * 修改收货地址信息
     *
     * @param detail 需修改的收货地址信息
     * @return 修改后的收货信息
     */
    @ApiOperation("修改收货地址信息")
    @PostMapping("/update")
    public CommonResult updateAddress(@RequestBody ReceiveAddressDetail detail) {
        if (userAddressService.updateAddress(detail)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 修改默认地址
     *
     * @param id 需修改为默认地址的地址ID
     * @return 修改救过
     */
    @ApiOperation("修改默认地址")
    @PostMapping("/setAsDefault")
    public CommonResult setAsDefault(@RequestParam(value = "id") Long id) {
        userAddressService.setAsDefault(id);
        return CommonResult.success();
    }

    /**
     * 添加新的收货地址
     *
     * @param detail 新的收货地址信息
     * @return 操作结果
     */
    @ApiOperation("添加收货地址")
    @PostMapping("/addAddress")
    public CommonResult addReceiveAddress(@RequestBody ReceiveAddressDetail detail) {
        userAddressService.addReceiveAddress(detail);
        return CommonResult.success();
    }
}
